package com.detrum.utils;

import android.content.Context;
import android.util.Base64;

import com.detrum.base.BaseStatic;
import com.detrum.base.R;
import com.detrum.bean.HttpBean;
import com.detrum.bean.VersionBean;
import com.detrum.bean.VersionBean_1;
import com.detrum.sqlite.SqliteVersionOperate;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


/**
 * Created by abc on 2018/1/15.
 * http获取版本管理类
 * 单独获取appVersion,deviceVersion
 * 同时获取两种版本
 */

public class HttpDataManager {
    private static HttpDataManager httpDataManager;
    public interface HttpManagerCall{
        void success();
        void fail(int msg);
    }

    private HttpDataManager() {
    }
    public static HttpDataManager getInstance(){
        synchronized (HttpDataManager.class){
            if(httpDataManager==null){
                httpDataManager = new HttpDataManager();
            }
            return httpDataManager;
        }
    }


    public void getVersion(final Context context, final String message, final HttpManagerCall httpManagerCall){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Gson gson = new Gson();
                byte[]json = null;
                try {
                    HttpBean httpBean = gson.fromJson(message,HttpBean.class);
                    int status = httpBean.getStatus();
                    if(status==0){
                        int length  = httpBean.getMessageLen();
                        String msg = httpBean.getMessage();
                        LogUtils.e("msg_length = "+length);
                        if(length==msg.length()){
                            json = Base64.decode(msg, Base64.DEFAULT);
                            LogUtils.e(new String(json));
                        }else{
                            httpManagerCall.fail(R.string.http_exception);//数据获取异常
                            return;
                        }
                    }else{
                        httpManagerCall.fail(R.string.http_exception);//数据获取异常
                        return;
                    }
                }catch (Exception e){
                    httpManagerCall.fail(R.string.web_fail);//数据获取失败
                }
                if(json!=null){
                    try {
                        JSONObject jsonObject = new JSONObject(new String(json));
                        String dataVersion = jsonObject.getString("version");
                        LogUtils.i("HttpDataManager dataVersion = "+dataVersion);
                        if(dataVersion.equals(BaseStatic.jsonVersion)){
                            VersionBean_1 versionBean1 = gson.fromJson(new String(json),VersionBean_1.class);
                            List<VersionBean_1.DataBean.AppBean> appBeans = versionBean1.getData().getApp();
                            if(appBeans==null||appBeans.size()==0){
                                return;
                            }
                            for (VersionBean_1.DataBean.AppBean appBean:appBeans
                                    ) {
                                if(appBean.getAppName()==null){
                                    continue;
                                }
                                if(appBean.getAppName().equals(BaseStatic.appName)){
                                    BaseStatic.appNetVersion = appBean.getAppVersion();
                                    BaseStatic.appNetCode = appBean.getAppCode();
                                    BaseStatic.appNetPath = appBean.getAppFilePath();
                                    BaseStatic.appNetLength = appBean.getAppFileLength();
                                    BaseStatic.appNetImagePath = appBean.getAppADImagePath();
                                    BaseStatic.appNetMessage = appBean.getAppUpdataMsg();
                                    BaseStatic.appNetMsgPath = appBean.getAppInstructionsFilePath();
                                    BaseStatic.appNetVerify = appBean.getAppVerify();
                                }
                                LogUtils.e(appBean.toString());
                            }
                            List<VersionBean_1.DataBean.DeviceBean> deviceBeans = versionBean1.getData().getDevice();
                            if(deviceBeans==null&&deviceBeans.size()==0){
                                return;
                            }
                            LogUtils.i("deviceBeans size = "+deviceBeans.size());
                            for (VersionBean_1.DataBean.DeviceBean deviceBean:deviceBeans
                                    ) {
                                if(deviceBean.getDeviceName()==null||deviceBean.getDeviceName().length()==0){
                                    LogUtils.i("deviceBean.getDeviceName() = null");
                                        continue;
                                }
                                LogUtils.i("deviceBean.getDeviceName() = "+deviceBean.getDeviceName());
                                VersionBean bean = new VersionBean();
                                bean.setName(deviceBean.getDeviceName());
                                bean.setNetVersion(deviceBean.getDeviceVersion());
                                bean.setFilePath(deviceBean.getSetupFilePath());
                                bean.setLength(deviceBean.getSetupFileLength());
                                bean.setVerify(deviceBean.getSetupFileVerify());
                                bean.setExplain(deviceBean.getExplain());
                                bean.setUpdataMsg(deviceBean.getUpdataMsg());
                                bean.setUpdataFilePath(deviceBean.getInstructionsFilePath());
                                bean.setImagePath(deviceBean.getADImagePath());
                                // TODO: 2018/3/9 必须制定版本号格式，保证可以转换为时间戳
                                LogUtils.i("deviceBean.getDeviceVersion() = "+deviceBean.getDeviceVersion());
                                String[]strs = deviceBean.getDeviceVersion().split("\\.");
                                String time = strs[strs.length-1];
                                LogUtils.i("time = "+time);
                                long timestamp = MyMethod.getTimeStamp(time,"yyyyMMddHHmmss");
                                bean.setUpdateTime(timestamp);
                                LogUtils.i("timestamp = "+timestamp);
                                LogUtils.i(bean.toString());
                                SqliteVersionOperate.getInstance(context).Insert(bean);
                            }
                            httpManagerCall.success();
                        }else{
                            // TODO: 2018/1/23 json版本号更改
                            LogUtils.i("json版本号更改 ");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        LogUtils.e("sadfadgasge");
                    }
                }
            }
        },"getVersion").start();

    }
}
